package com.minazuki.signinserver.web;

import com.minazuki.signinserver.config.MqttProperties;
import com.minazuki.signinserver.mqtt.Mqtt;
import com.minazuki.signinserver.pojo.msg.Msg;
import com.minazuki.signinserver.service.SigninService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("signin")
@RestController
public class SigninController {
    private static final Logger logger = LoggerFactory.getLogger(SigninController.class);

    @Autowired
    SigninService ss;
    @Autowired
    MqttProperties config;
    @Autowired
    private Mqtt.MqttGateway mqtt;

    @PostMapping("sign/{pid}")
    public ResponseEntity<Msg> addSign(@PathVariable int pid, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        boolean result = ss.addSign(pid, token);
        if (result) {
            msg.setMsg("签到成功");
        } else msg.setMsg("签到失败");
        return ResponseEntity.ok(msg);
    }

    @GetMapping("users/{pid}")
    public ResponseEntity<Msg> getSign(@PathVariable int pid, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();

        List result = ss.getSignedUser(pid, token);
        if (result.isEmpty()) {
            msg.setMsg("空");
        } else {
            msg.setMsg("");
            msg.setSigninRecords(result);
        }
        return ResponseEntity.ok(msg);
    }

    @GetMapping("check/{pid}")
    public ResponseEntity<Msg> checkSign(@PathVariable int pid, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();

        boolean result = ss.checkSign(pid, token);
        if (result) {
            msg.setMsg("已签到");
        } else {
            msg.setMsg("");
        }
        return ResponseEntity.ok(msg);
    }
}
